/**
 * @(#) AccountProxy.as
 *
 * @date		2010. 11. 01
 * @version		2.0
 * @author		Shin, Yunwoo
 * @client		Service Ace
 * @module		CongMoney  (경조금)
 * @description	경조 신청 계좌 저장, 등록시 은행 조회- Proxy
 *
 */
package com.skt.common_hr.schoolexp_nos.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.schoolexp_nos.model.vo.AccountVO;
	import com.wcl.dataloader.*;

	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class AccountProxy extends CommonProxy
	{
		public static const NAME:String = "AccountProxy";

		public static const LOAD_ACCOUNTLIST_COMPLETE:String = "loadAccountListComplete";
		public static const LOAD_ACCOUNTLIST_FAILED:String = "loadAccountListFailed";
		public static const LOAD_ACCOUNT_SAVE_COMPLETE:String = "loadAccountSaveComplete";
		public static const LOAD_ACCOUNT_SAVE_FAILED:String = "loadAccountSaveFailed";
		public static const LOAD_BANKCODE_COMPLETE:String = "loadBankCodeComplete";
		public static const LOAD_BANKCODE_FAILED:String = "loadBankCodeFailed";

		public var AccountBank:ArrayCollection = new ArrayCollection();
		public var userAccountData:ArrayCollection = new ArrayCollection();
		public var errMessage:String;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function AccountProxy(data:Object=null)
		{
			super(NAME, data);
		}


		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 기타계좌 콤보박스 조회
		 * @param
		 * @return void
		 */
		public function loadBankList():void
		{
			var param:Object = new Object();
			loadData("Z_HR_0051",param, bankCodeResult, bankCodeFault);
		}

		public function load(pernr:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;

			loadData("Z_HR_0042",param, result, fault);
		}

		public function save(pernr:String, vo:AccountVO):void
		{

			var param:Object = new Object();
			param["I_PERNR"] = pernr;

			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(vo);
			tables["BTAB"] = table;

			saveData("Z_HR_0049", param, tables, saveResult, saveFault);
		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 기타계좌 콤보박스 데이터
		 *
		 * @param
		 * @return void
		 */
		public function bankCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				AccountBank = bankCodeList(event.result.BTAB);
				facade.sendNotification(LOAD_BANKCODE_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_BANKCODE_FAILED, this);
			}
		}
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {
				userAccountData = accountControlList(event.result.BTAB);
				facade.sendNotification(LOAD_ACCOUNTLIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_ACCOUNTLIST_FAILED, this);
			}
		}

		private function fault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_ACCOUNTLIST_FAILED, this);
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function bankCodeFault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_BANKCODE_FAILED, this);
		}

		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 기타계좌 콤보박스 데이터
		 *
		 * @param
		 * @return void
		 */
		public function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(LOAD_ACCOUNT_SAVE_COMPLETE, this);
			}
			else {
				errMessage = event.result["RETTEXT"]
				facade.sendNotification(LOAD_ACCOUNT_SAVE_FAILED, this);
			}
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function saveFault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_ACCOUNT_SAVE_FAILED, this);
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 선택적복리후생 초기화면 list
		 */
		private function bankCodeList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var rec:Object in srcArr)
			{
				retArr.addItem({label:rec["BANKA"], data:rec["BANKL"]});
			}
			return retArr;
		}

		/****
		 * 계좌관리 메인데이터 조회
		 * ***/
		private function accountControlList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var rec:Object in srcArr)
			{
				var vo:AccountVO = new AccountVO();
				vo.BNKSA = rec["BNKSA"];
				vo.STEXT = rec["STEXT"];
				vo.BANKL = rec["BANKL"];
				vo.BANKA = rec["BANKA"];
				vo.BANKN = rec["BANKN"];
				vo.EMFTX = rec["EMFTX"];
				vo.BANKS = rec["BANKS"];

				retArr.addItem(vo);
			}
			return retArr;
		}

	}
}